//
//  ImagesCollectionViewController.swift
//  MKPhotoDetailDemo
//
//  Created by 劢克科技 on 2019/3/22.
//  Copyright © 2019 劢克科技. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class ImagesCollectionViewController: UICollectionViewController {

    let photos = [#imageLiteral(resourceName: "屏幕快照 2019-02-12 上午9.40.00"), #imageLiteral(resourceName: "屏幕快照 2019-02-01 下午5.29.42"), #imageLiteral(resourceName: "屏幕快照 2019-02-01 下午4.58.08"), #imageLiteral(resourceName: "141553238941_.pic_hd"), #imageLiteral(resourceName: "111553238939_.pic_hd"), #imageLiteral(resourceName: "71553238935_.pic_hd"), #imageLiteral(resourceName: "81553238936_.pic_hd"), #imageLiteral(resourceName: "101553238938_.pic_hd"), #imageLiteral(resourceName: "91553238937_.pic_hd"), #imageLiteral(resourceName: "131553238940_.pic_hd"), #imageLiteral(resourceName: "121553238940_.pic_hd"), #imageLiteral(resourceName: "e65d28dcfc6d80160082a1ba4a413fc586727ff076368-2ZDdLk_fw658"), #imageLiteral(resourceName: "timg")]
    var imagesDatas = [MKImageData]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.register(ImagesCollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)

        // Do any additional setup after loading the view.
        setUI()
    }

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return photos.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! ImagesCollectionViewCell
    
        cell.imageView.image = photos[indexPath.item]
        let data = MKImageData()
        data.browseImageView = UIImageView(image: cell.imageView.image!)
        print(indexPath.item / 2)
        if Double(indexPath.item) / 2 - Double(indexPath.item / 2) == 0 {
            data.textInfo = true
        }
        imagesDatas.append(data)
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        var imageDatas = [MKImageData]()
        
        // 添加图片数据
        let imageData = MKImageData()
        imageData.imageUrl = URL(string: "http://b.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8ef5250cec5134954082376c9.jpg")
        imageDatas.append(imageData)
        
        // 添加图片+文字数据
        let imageTextData = MKImageData()
        imageTextData.imageUrl = URL(string: "http://b.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8ef5250cec5134954082376c9.jpg")
        imageTextData.setupTextInfo(title: "Title Name", desc: "Description for Image.", content: "This is content area. MKMK Haaa...Happy every day!", date: "2019年6月1日")
        imageDatas.append(imageTextData)
        
        // 添加视频数据
        let videoData = MKImageData()
        videoData.videoUrl = URL(string: "http://note.meckey.com/uploads/20190228/1542275a16bce575f3ddce9c819de125ba0029.mp4")
        imageDatas.append(videoData)
        
        // 展示控制器
        let controller = MKImageBrowser()
        controller.imageDatas = imageDatas
        controller.currentIndex = 0
        controller.delegate = self
        present(controller, animated: true, completion: nil)
    }
}

extension ImagesCollectionViewController: MKImageBrowserDelegate {
    func handleBtnClick(index: Int) {
        print("click")
    }
    
    func handleBtnClick() {
        print("点击了按钮")
    }
}

//MARK: - UI

extension ImagesCollectionViewController {
    func setUI() {
        title = "点击查看大图"
        collectionView.backgroundColor = UIColor.white
    }
}
